/**
 * Created by charsen on 16/5/27.
 */
directives.directive('auxiliaryLine', ["$rootScope", "tools", "$timeout", "$document", function ($rootScope, tools, $timeout, $document) {
    return {
        restrict: 'A',
        scope: false,
        link: function (scope, element, attr) {
            // 绘制辅助线
            scope.drawAuxiliaryLine = function (flag, $ele) {
                if (!flag) {
                    $('.chart-manager-container .auxiliaryLine_hor_bottom').addClass('none');
                    $('.chart-manager-container .auxiliaryLine_ver_right').addClass('none');
                    $('.chart-manager-container .auxiliaryLine_ver_left').addClass('none');
                    $('.chart-manager-container .auxiliaryLine_hor_top').addClass('none');
                    return;
                } else {
                    var element_index = $ele.index();
                    var left = $ele.position().left;
                    var top = $ele.position().top;
                    var height = $ele.height();
                    var width = $ele.width();
                }

                var bottom_found = false;
                var right_found = false;
                var left_found = false;
                var top_found = false;

                $('.chart-manager-container .charts-container > div').each(function (index, ele) {
                    if (element_index == index) {
                        return;
                    }

                    var ele_left = $(ele).position().left;
                    var ele_top = $(ele).position().top;

                    // 绘制顶部辅助线
                    if (Math.abs(ele_top - top) < 10) {
                        $('.chart-manager-container .auxiliaryLine_hor_top').removeClass('none').css('top', top + 'px');
                        top_found = true;

                        if (Math.abs(ele_top - top) < 0.5) {
                            $('.chart-manager-container .auxiliaryLine_hor_top').addClass('active');
                        } else {
                            $('.chart-manager-container .auxiliaryLine_hor_top').removeClass('active');
                        }
                    }

                    // 绘制底部辅助线
                    if (Math.abs(ele_top + $(ele).height() - (top + height)) < 10) {
                        $('.chart-manager-container .auxiliaryLine_hor_bottom').removeClass('none').css('top', top + height + 'px');
                        bottom_found = true;

                        if (Math.abs(ele_top + $(ele).height() - (top + height)) < 0.5) {
                            $('.chart-manager-container .auxiliaryLine_hor_bottom').addClass('active');
                        } else {
                            $('.chart-manager-container .auxiliaryLine_hor_bottom').removeClass('active');
                        }
                    }

                    // 绘制左边辅助线
                    if (Math.abs(ele_left - left) < 10) {
                        $('.chart-manager-container .auxiliaryLine_ver_left').removeClass('none').css('left', left + 'px');
                        left_found = true;

                        if (Math.abs(ele_left - left) < 0.5) {
                            $('.chart-manager-container .auxiliaryLine_ver_left').addClass('active');
                        } else {
                            $('.chart-manager-container .auxiliaryLine_ver_left').removeClass('active');
                        }
                    }

                    // 绘制右边辅助线
                    if (Math.abs(ele_left + $(ele).width() - (left + width)) < 10) {
                        $('.chart-manager-container .auxiliaryLine_ver_right').removeClass('none').css('left', left + width + 'px');
                        right_found = true;

                        if (Math.abs(ele_left + $(ele).width() - (left + width)) < 0.5) {
                            $('.chart-manager-container .auxiliaryLine_ver_right').addClass('active');
                        } else {
                            $('.chart-manager-container .auxiliaryLine_ver_right').removeClass('active');
                        }
                    }
                });

                if (!top_found) {
                    $('.chart-manager-container .auxiliaryLine_hor_top').addClass('none');
                }
                if (!bottom_found) {
                    $('.chart-manager-container .auxiliaryLine_hor_bottom').addClass('none');
                }
                if (!left_found) {
                    $('.chart-manager-container .auxiliaryLine_ver_left').addClass('none');
                }
                if (!right_found) {
                    $('.chart-manager-container .auxiliaryLine_ver_right').addClass('none');
                }
            }
        }
    }
}]);